package com.w.wx.service.impls;


import cn.hutool.core.date.DateTime;
import com.w.wx.domain.*;

import com.w.wx.mapper.*;
import com.w.wx.service.ImagesService;
import com.w.wx.utils.UUIDUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

@Slf4j
@Service
public class ImagesServiceImpl implements ImagesService {

    @Value("${web.localurl}")
    private String realPath;
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private ImagesMapper imagesMapper;
    @Autowired
    private NoticeMapper noticeMapper;
    @Autowired
    private CommentsInfoMapper commentsInfoMapper;
    @Autowired
    private CommentsReplyMapper commentsReplyMapper;
    @Autowired
    private UserErMapper userErMapper;


    @Override
    public int setImageInfo(HttpServletRequest request, MultipartFile file, String openid,Integer flag) throws IOException {
        //物品图片信息
        Images images = new Images();//封装图片信息
        images.setState(0);//待审核
        Integer gid = goodsMapper.selectByOpenidForGid(openid);
        if (flag == 0){
            //从数据库获取商品gid,发布物品图片
            if(gid==0){
                return 0;
            }else{
                images.setGid(gid);
            }
        }
        else{
            //学生认证时图片,待审核为3
            userErMapper.updateByOpenid(openid,3);
            images.setOpenid(openid);
        }
        images.setImageAddress(realPath);//服务器存放图片真实路径
        String path ;
        String type ;
        String avator;
        if(!file.isEmpty()) {
            String fileName = file.getOriginalFilename();
            images.setImageName(fileName);//图片真实名称，但是小程序获取的是临时文件存放图片路径
            type = fileName.indexOf(".") != -1 ? fileName.substring(fileName.lastIndexOf(".") + 1) : null;
            if (type != null) {
                    // 自定义的文件名称
                    Calendar rightNow=Calendar.getInstance();
                    Integer year = rightNow.get(Calendar.YEAR);
                    Integer month = rightNow.get(Calendar.MONTH)+1; //第一个月从0开始，所以得到月份＋1
                    Integer day = rightNow.get(rightNow.DAY_OF_MONTH);

                    String date=year+""+month+""+day;
                    String trueFileName = UUIDUtils.getUUID()+date+".jpg";//把图片都变成jpg格式

                    // 设置存放图片文件的路径
                    path = realPath +trueFileName;
                    //判断文件父目录是否存在
                    File dest=new File(path);
                    if(!dest.getParentFile().exists()){
                        dest.getParentFile().mkdir();
                    }
                    //保存文件
                    file.transferTo(new File(path));
                    avator=request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + "/images/" + trueFileName;
                    images.setImageUrl(avator);//可访问的图片url地址
                    if (flag==0){
                        //只有发布物品时会更新物品信息，学生认证时不更新
                        goodsMapper.updateGoodsHomeImage(avator,gid);
                    }
                    log.info("数据库存放图片文件的路径:" + avator);
            }else {
                log.info("文件类型为空");
                return 0;
            }
        }else {
            log.info("没有找到相对应的文件");
            return 0;
        }
        //写入数据库
        return imagesMapper.insert(images);
    }

    @Override
    public ArrayList<String> getImagesByGid(Integer gid) {

        return imagesMapper.selectByGid(gid);
    }

    @Override
    public ArrayList<Notice> getAllNoticeInfo() {

        return noticeMapper.selectAll();
    }

    @Override
    public int deleteNotice(Integer mid) {

        return noticeMapper.deleteByPrimaryKey(mid);
    }

    @Override
    public int updateNotice(Integer mid, String noticeInfo) {

        Notice notice = new Notice();
        notice.setMid(mid);
        notice.setNoticeInfo(noticeInfo);
        notice.setSendTime(new DateTime());
        return noticeMapper.updateByPrimaryKey(notice);
    }

    @Override
    public void addGooddNotice(CommentsInfo info) {
        Goods goods = goodsMapper.selectDetailByGid(info.getGoodId());
        Notice notice = new Notice();
        notice.setSendTime(new DateTime());
        notice.setGid(info.getGoodId());
        notice.setFromOpenid(info.getFromOpenid());
        notice.setToOpenid(goods.getOpenid());
        notice.setNoticeInfo(info.getContent());

        noticeMapper.insert(notice);
    }

    @Override
    public void addLikedNotice(Integer objId, String openid, String type) {
        //有人点赞就通知
        Integer integer = Integer.valueOf(type);
        Notice notice = new Notice();
        notice.setFromOpenid(openid);
        notice.setSendTime(new DateTime());

        if(integer == 0){
            //点赞了物品
            Goods goods = goodsMapper.selectDetailByGid(objId);
            notice.setGid(goods.getGid());
            notice.setNoticeInfo("liked");
            notice.setToOpenid(goods.getOpenid());
        }else if (integer == 1){
            //点赞了主评论
            CommentsInfo commentsInfo = commentsInfoMapper.selectByPrimaryKey(objId);
            notice.setToOpenid(commentsInfo.getFromOpenid());
            notice.setNoticeInfo("liked");
            notice.setGid(commentsInfo.getGoodId());
        }else {
            //点赞了子评论
            CommentsReply commentsReply = commentsReplyMapper.selectByPrimaryKey(objId);
            notice.setToOpenid(commentsReply.getFromOpenid());
            notice.setNoticeInfo("liked");
            notice.setGid(-1);
        }

        noticeMapper.insert(notice);

    }


}
